﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;
using SportsStore.Domain;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Controllers
{
    public class ProductController : Controller
    {
        public int PageSize = 1;
        private IProductRepository repo;

        public ProductController(IProductRepository productrepo)
        {
            repo = productrepo;
        }


        public ViewResult List(string category, int page = 1)
        {
            ProductsListViewModel viewModel = new ProductsListViewModel
            {
                Products = repo.Products
                .Where(p => category == null ? true : p.Category == category) 
                .OrderBy(p => p.ProductID)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PageData = new PagingInfo{
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = category == null ? repo.Products.Count() : repo.Products.Where(c => c.Category == category).Count()
                },
                CurrentCategory = category
            };

            return View(viewModel);
        }
        
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        public FileContentResult GetImage(int productId)
        {
            Product prod = repo.Products.FirstOrDefault(p => p.ProductID == productId); 
            if (prod != null)
            {
                return File(prod.ImageData, prod.ImageMimeType);
            }
            else
            {
                return null;
            }
        } 

    }
}
